﻿// Copyright (c) Inossem. All rights reserved.
// Licensed under the LGPL-3.0 license. See LICENSE file in the project root for full license information.
using DWF.Studio.Custom;
using DWF.Studio.Share.Log;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace DWF.Studio.Views
{
    /// <summary>
    /// loading窗体
    /// </summary>
    public partial class WindowLoading : Window
    {
        /// <summary>
        /// loading窗体引用
        /// </summary>
        private static WindowLoading _windowLoading = null;

        private static AutoResetEvent _autoResetEvent = new AutoResetEvent(false);

        /// <summary>
        /// 是否已经显示
        /// </summary>
        private static bool _isShowed = false;

        private static object _obj = new object();

        /// <summary>
        /// 显示窗体
        /// </summary>
        /// <param name="title"></param>
        /// <param name="ownerWindow"></param>
        public static void ShowWindow(string title, Window ownerWindow)
        {
            double? parentX = null;
            double? parentY = null;
            double? parentWidth = null;
            double? parentHeight = null;
            var isMaximized = true;
            if (ownerWindow != null)
            {
                try
                {
                    var location = ownerWindow.GetAbsolutePosition();
                    parentX = location.X;
                    parentY = location.Y;
                    parentWidth = ownerWindow.ActualWidth;
                    parentHeight = ownerWindow.ActualHeight;
                }
                catch (Exception ex)
                {
                    LogHelper.Error(ex);
                }

                isMaximized = ownerWindow.WindowState == WindowState.Maximized;//是否是最大化
            }

            _isShowed = true;
            Thread thread = new Thread(new ThreadStart(() =>
            {
                _windowLoading = new WindowLoading();

                //计算窗体位置 如果是最大化 直接使用WindowStartupLocation.CenterScreen 否则多屏幕时 计算错误
                if (isMaximized || !(parentX.HasValue && parentY.HasValue && parentWidth.HasValue && parentHeight.HasValue))
                {
                    _windowLoading.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                }
                else
                {
                    var width = parentWidth.Value - _windowLoading.Width;
                    var height = parentHeight.Value - _windowLoading.Height;
                    _windowLoading.Left = parentX.Value + width / 2f;
                    _windowLoading.Top = parentY.Value + height / 2f;
                }

                _autoResetEvent.Set();
                Thread.Sleep(300);
                if (_windowLoading != null)
                {
                    _windowLoading.tblTitle.Text = title;
                    _windowLoading.tblMsg.Text = String.Empty;
                    _windowLoading.ShowDialog();

                    lock (_obj)
                    {
                        _windowLoading.Dispatcher.InvokeShutdown();//防止线程实力无法退出
                        _windowLoading = null;
                    }
                }
            }));

            thread.SetApartmentState(ApartmentState.STA);
            thread.IsBackground = false;
            thread.Priority = ThreadPriority.AboveNormal;
            thread.Start();
        }

        /// <summary>
        /// 关闭窗体
        /// </summary>
        public static void CloseWindow()
        {
            if (_isShowed)
            {
                _autoResetEvent.WaitOne();
            }

            lock (_obj)
            {
                if (_windowLoading != null)
                {
                    _windowLoading.Dispatcher.Invoke(() =>
                   {
                       _windowLoading.Close();
                   });
                }
            }

            _isShowed = false;
        }

        public static void SetContent(string title, string msg = "")
        {
            Thread.Sleep(100);
            if (_windowLoading != null)
            {
                _windowLoading.Dispatcher.Invoke(() =>
                {
                    _windowLoading.tblTitle.Text = title;
                    _windowLoading.tblMsg.Text = msg;
                });
            }
        }

        public static void SetMsg(string msg = "")
        {
            //Thread.Sleep(10);
            if (_windowLoading != null)
            {
                _windowLoading.Dispatcher.Invoke(() =>
                {
                    _windowLoading.tblMsg.Text = msg;
                });
            }
        }

        private WindowLoading()
        {
            InitializeComponent();
        }

        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.System && e.SystemKey == Key.F4)
            {
                e.Handled = true;
            }
        }
    }
}
